package com.ydc.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ydc.mapper.BillMapper;
import com.ydc.mapper.ItemMapper;
import com.ydc.mapper.UserMapper;
import com.ydc.pojo.Bill;
import com.ydc.pojo.PageResult;
import com.ydc.pojo.Item;
import com.ydc.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;


@Service
public class BillServiceImpl implements BillService {
    @Autowired
    private BillMapper billMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private ItemMapper itemMapper;
    //前端页面订单详情
    @Override
    public PageResult getuserbill(PageResult pageResult) {
        IPage page = new Page(pageResult.getPageNum(), pageResult.getPageSize());
        QueryWrapper<Bill> queryWrapper = new QueryWrapper<>();
        boolean flag = StringUtils.hasLength(pageResult.getQuery());
        queryWrapper.like(flag,"item_type", pageResult.getQuery())
                .eq("user_id", pageResult.getId());
        page = billMapper.selectPage(page,queryWrapper);
        long total = page.getTotal();
        List<Bill> billList = page.getRecords();

        return pageResult.setTotal(total).setRows(billList);
    }

    //后端页面订单展示
    @Override
    public PageResult findBillByPage(PageResult pageResult) {
        IPage page = new Page(pageResult.getPageNum(), pageResult.getPageSize());
        QueryWrapper<Bill> queryWrapper = new QueryWrapper<>();
        boolean flag = StringUtils.hasLength(pageResult.getQuery());
        queryWrapper.like(flag,"user_realname", pageResult.getQuery());
        page = billMapper.selectPage(page,queryWrapper);
        long total = page.getTotal();
        List<Bill> billList = page.getRecords();
        return pageResult.setTotal(total).setRows(billList);
    }

    //前台下单
    @Override
    synchronized public void addBill(Bill bill) {
        Item item = itemMapper.selectOne(new QueryWrapper<Item>().eq("id", bill.getItemId()));

        if (bill.getUserId()!=null) {
            User user = userMapper.selectOne(new QueryWrapper<User>().eq("id", bill.getUserId()));
            bill.setUserPhone(user.getPhone()).setUserRealname(user.getRealname()).setMsg(bill.getMsg());
        }else {
            User user = userMapper.selectOne(new QueryWrapper<User>().eq("realname", bill.getUserRealname()));
            if (user!=null) bill.setUserId(user.getId());
        }

        bill.setItemNum(item.getItemNum()).setItemType(item.getItemType());
        bill.setStatus(true).setMoney(item.getPrice());

        itemMapper.updateById(item);

        billMapper.insert(bill);
    }

    //后台结账操作
    @Override
    public void updateBillStatus(Bill bill) {
       // Bill bill1 = billMapper.selectById(bill.getId());
//        itemMapper.updateById(
//                new Item().setId(bill1.getItemId()).setStatus(false));

        bill.setStatus(false);
        billMapper.updateById(bill);
    }
    //后台通过id删除订单
    @Override
    public void deleteBillById(Bill bill) {
        billMapper.deleteById(bill);
    }
    //后台更新订单
    @Override
    public void updateBill(Bill bill) {
        billMapper.updateById(bill);
    }
}
